// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import {ContractList} from '../domains/contractlist.domain';
import {Contract} from '../domains/contract.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class ContractServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'contractServiceSvr.assx/';
  }


  saveContract(token: string, customerId: string, month: number, houseId: string, startTime: string, payPeriod: number, remark: string): Promise<Contract> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString())
      .append('month', month.toString())
      .append('houseId', houseId.toString())
      .append('startTime', startTime.toString())
      .append('payPeriod', payPeriod.toString())
      .append('remark', remark.toString());
    return this.request.post<Contract>(this.baseUrl + 'saveContract', httpParams).then((res) => {
      return new Contract(res);
    });
  }


  getContractByPrimaryKey(token: string, contractId: string): Promise<Contract> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('contractId', contractId.toString());
    return this.request.post<Contract>(this.baseUrl + 'getContractByPrimaryKey', httpParams).then((res) => {
      return new Contract(res);
    });
  }


  getDiscountPrice(customerId: string, rent: string): Promise<string> {
    const httpParams = new HttpParams()
      .append('customerId', customerId.toString())
      .append('rent', rent.toString());
    return this.request.post<string>(this.baseUrl + 'getDiscountPrice', httpParams).then((res) => {
      return res;
    });
  }


  listContracts(token: string, isViolation: number, state: number, contractTemplateName: string, startTime: Date, endTime: Date, customerName: string, skip: number, size: number): Promise<ContractList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('isViolation', isViolation.toString())
      .append('state', state.toString())
      .append('contractTemplateName', contractTemplateName.toString())
      .append('startTime', startTime.toString())
      .append('endTime', endTime.toString())
      .append('customerName', customerName.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<ContractList>(this.baseUrl + 'listContracts', httpParams).then((res) => {
      return new ContractList(res);
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  updateContractState(token: string, contractId: string, isViolation: number, state: number): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('contractId', contractId.toString())
      .append('isViolation', isViolation.toString())
      .append('state', state.toString());
    return this.request.post<number>(this.baseUrl + 'updateContractState', httpParams).then((res) => {
      return res;
    });
  }

  getContractUrl(token: string, houseId: string, customerId: string): Promise<string> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('houseId', houseId.toString())
      .append('customerId', customerId.toString());
    return this.request.post<string>(this.baseUrl + 'getContractUrl', httpParams).then((res) => {
      return res;
    });
  }
}
